package com.tjp.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.tjp.subject.application.convert.SubjectCategoryBOConverter;
import com.tjp.subject.application.convert.SubjectInfoBOConverter;
import com.tjp.subject.application.entity.SubjectCategoryDTO;
import com.tjp.subject.application.entity.SubjectInfoDTO;
import com.tjp.subject.common.entity.JZResult;
import com.tjp.subject.common.entity.PageResult;
import com.tjp.subject.domain.convert.SubjectInfoConverter;
import com.tjp.subject.domain.entity.SubjectCategoryBO;
import com.tjp.subject.domain.entity.SubjectInfoBO;
import com.tjp.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: jz-club
 * @description: 题目信息控制器
 * @author: aaronTang
 * @create: 2024-02-22
 */
@RestController
@RequestMapping("/subject/info")
@Slf4j
public class SubjectInfoController {

    @Autowired
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    public JZResult<Boolean> insert(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.insert.SubjectInfoDTO is {}", JSON.toJSONString(subjectInfoDTO));
            }

            // 确保 subjectName 不为空
            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectInfoDTO.getSubjectName()), "题目名称不能为空");

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");

            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectInfoDTO.getSettleName()), "出题人不能为空");

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoBOConverter.INSTANCE.subjectInfoConvertDTOToBO(subjectInfoDTO);

            subjectInfoDomainService.insert(subjectInfoBO);
            return JZResult.ok();
        }catch (Exception e) {
            log.error("SubjectCategoryController.insert.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }


    /**
     * 查询题目分页列表
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public JZResult<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectPage.subjectInfoDTO is {}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");

            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoBOConverter.INSTANCE.subjectInfoConvertDTOToBO(subjectInfoDTO);

            PageResult<SubjectInfoBO> pageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);

            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectPage.pageResult is {}", JSON.toJSONString(pageResult));
            }
            return JZResult.ok(pageResult);
        }catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPage.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }


    /**
     * 查询题目信息
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public JZResult<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectInfo.subjectInfoDTO is {}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getId(), "id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoBOConverter.INSTANCE.subjectInfoConvertDTOToBO(subjectInfoDTO);

            //查询信息
            SubjectInfoBO resultBO = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);

            SubjectInfoDTO resultDTO = SubjectInfoBOConverter.INSTANCE.convertSubjectInfoBOToDTO(resultBO);

            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectInfo.resultDTO is {}", JSON.toJSONString(resultDTO));
            }
            return JZResult.ok(resultDTO);
        }catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectInfo.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }








}
